package com.smart.manager.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.core.Page;
import com.smart.manager.system.entities.body.RSystemApi;
import com.smart.manager.system.entities.models.SystemApi;
import com.smart.manager.system.repository.ApiRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;


/**
 * @author Administrator
 */

@Api(tags = "接口管理")
@RestController
@RequestMapping(SmartConstant.MANAGER_API_PREFIX + "system/api")
@RequiredArgsConstructor
public class ApiApi {

    private final ApiRepository apiService;


    @GetMapping(value = "pageList")
    @ApiOperation(value = "查询接口")
    @SaCheckPermission("system:api:pageList")
    public AjaxResult<Page<SystemApi>> pageList(@Validated RSystemApi.SearchBody body) {
        Page<SystemApi> result = apiService.pageList(body);
        return AjaxResult.data(result);
    }


    @PostMapping(value = "add")
    @ApiOperation(value = "新增接口")
    @SaCheckPermission("system:api:add")
    public AjaxResult<String> add(@RequestBody @Validated RSystemApi.EditBody body) {
        apiService.add(body);
        return AjaxResult.success();
    }


    @PutMapping(value = "update/{id}")
    @ApiOperation(value = "修改接口")
    @SaCheckPermission("system:api:update")
    public AjaxResult<String> update(@PathVariable Long id, @RequestBody @Validated RSystemApi.EditBody body) {
        apiService.update(id, body);
        return AjaxResult.success();
    }


    @DeleteMapping(value = "delete")
    @ApiOperation(value = "删除接口")
    @SaCheckPermission("system:api:delete")
    public AjaxResult<Integer> delete(@RequestBody @NotEmpty(message = ArcoTips.PK_EMPTY) List<Long> ids) {
        int x = apiService.delByIds(ids);
        return AjaxResult.delSuccess(x);
    }


    @PostMapping(value = "scan")
    @ApiOperation(value = "扫描接口")
    @SaCheckPermission("system:api:scan")
    public AjaxResult<String> scan() {
        apiService.scanApi();
        return AjaxResult.success();
    }

    @PostMapping(value = "download")
    @ApiOperation(value = "资源下载")
    @SaCheckPermission("system:api:download")
    public AjaxResult<String> download() {
        String res = apiService.download();
        return AjaxResult.data(res);
    }
}
